define(function(require, exports, module){
	var ELView=require("elview");
	var aw = require("ajaxwrapper");
	var Form=require("form");
	var Subnav = require("subnav"); 
	var Dialog=require("dialog");
	var Grid = require("grid");
	var user = require("store").get("user");
	
	var template="<div class='el-dishtype'>"+
	 "<div class='J-subnav'></div>"+
	 "<div class='J-grid'></div>"+
	 "<div class='J-form hidden'></div>"+
	 "</div>";
	
	var dishtype = ELView.extend({
		attrs:{
        	template:template
        },
		events:{
			
	
		},
		initComponent:function(params,widget){
			var subnav=new Subnav({
        		parentNode:".J-subnav",
				model : {
					title:"菜品类型档案",
					items : [{
						placeholder:"类型",
						id : "search",
						type : "search",
						handler : function(str) {
							var g = widget.get("grid");
							var subnav=widget.get("subnav");
							var obj={s:str,
									properties:"name",
	            					fetchProperties:"*,servicePoint.*"
	    	        				};
							aw.ajax({
								url:"api/api/cater/dishtype/search",
								data:obj,
								dataType:"json",
								success:function(data){
									g.setData(data);
								}
							});
						}
					},{
						id:"servicePoint",
						type:"buttongroup",
						all:{
							show:false,
							first:true
						},
						tip:"服务点",
						keyField :"pkServicePoint",
						valueField :"name",
						items:user.privateServicePoints,
						params: function(){
							return{
								fetchProperties:"pkServicePoint,name"
							}
						},
						handler:function(key,element){
							widget.get("grid").refresh();
						}
					},{
						id:"add",
						text:"新增",
						type:"button",
						handler:function(){
							widget.get("form").reset();
							widget.show([".J-form"]).hide([".J-grid"]);
							widget.get("subnav").hide(["add","search"]).show(["return"]);
						}
					},{
						id:"return",
						type:"button",
						text:"返回",
						show : false,
						handler:function(){
							widget.show([".J-grid"]).hide([".J-form"]);
							widget.get("subnav").hide(["return"]).show(["add","search"]);
							widget.get("grid").refresh();
						}
					}],
				}
        	});
        	this.set("subnav",subnav);
        	var grid=new Grid({
        		parentNode:".J-grid",
        		url : "api/cater/dishtype/query",
        		autoRender : false,
        		params : function() {
    				var subnav=widget.get("subnav");
    				var obj={
    						"servicePoint":widget.get("subnav").getValue("servicePoint"),
    						fetchProperties:"*,servicePoint.*"
	        				};
    				return obj;
    			},
        		model : {
        			columns : [{
						name:"iOrder",
//						className:"name",
						label:"序号"
					},{
						name:"name",
//        				className:"detial",
        				label:"类型"
					},{
						name:"remark",
//        				className:"detial",
        				label:"备注 "
					},{
						key:"operate",
//						className:"operate",
						name:"操作",
						format:"button",
						formatparams:[{
							key:"edit",
							//icon:"icon-edit",
							text:"编辑",
							handler:function(index,data,rowEle){
								var grid=widget.get("grid");
								var form = widget.get("form");
								form.reset();
								form.setData(data);
								form.setValue("servicePoint",data.servicePoint.pkServicePoint);
								widget.hide([".J-grid"]).show([".J-form"]);
								widget.get("subnav").hide(["add","search"]).show(["return"]);
								widget.get("grid").refresh();
							}
						},{
							key:"delete",
							//icon:"icon-remove",
							text:"删除",
							handler:function(index,data,rowEle){
								aw.del("api/cater/dishtype/"+data.pkDishType+"/delete",function() {
									grid.refresh();
								});	
							}
						
						}]
					}]
    			}
    		 });
    		 this.set("grid",grid);
    		 $(".text-center  ").eq(5).attr("style","width:200px;"); 
    		 var form=new Form({
         		 parentNode:".J-form",
         		 saveaction : function() {
          			var data=widget.get("form").getData();
          			data.servicePoint=widget.get("subnav").getValue("servicePoint");
          			if(data.iOrder) {
          				data.iOrder=parseInt(data.iOrder);
          			}
          			aw.saveOrUpdate("api/cater/dishtype/save",data,function(data){
          				widget.hide([".J-form"]).show([".J-grid"]);
     					widget.get("subnav").show(["add","search"]).hide(["return"]);
     					widget.get("grid").refresh();
     				});
     			 },
     			 cancelaction:function(){
     				 	widget.get("grid").refresh();
     				 	widget.show([".J-grid"]).hide([".J-form"]);
     				 	widget.get("subnav").show(["add","search"]).hide(["return"]);
     					return false;
     			 },
	  			 model:{
	 				id:"dishType",
					items:[{
							name:"pkDishType",
							type:"hidden"
						},{
							name : "version",
							defaultValue : "0",
							type : "hidden"
						},{
							name:"servicePoint",
							type:"hidden"
						},{
							name:"iOrder",
							label:"序号",
							exValidate: function(value){
								 var type="^\\d+$"; 
							     var reg = new RegExp(type); 
	                        	 if (value && !reg.test(value)) {
	                                 return "请输入0或正整数";
	                             } else {
	                                 return true;
	                             }
	        				}
						},{
							name:"name",
							label:"菜品类型",
							validate:["required"]
						},{
							name:"remark",
							label:"备注",
							type:"textarea",
							height:100
						}]
			    }
        	 });
        	 this.set("form",form);
		},
		afterInitComponent:function(params,widget){
//        	aw.ajax({
//				url : "api/servicepoint/operableservicepoints",
//				dataType:"json",
//				data : {},
//				success:function(data){
//					widget.get("subnav").setData("servicePoint",data);
					widget.get("grid").refresh();
//				}
//        	});
        }
	});
	module.exports = dishtype;	
	
});